package com.example.jinzproduct.server.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.commons.bo.LogBO;
import com.example.commons.entity.LogDO;
import com.example.commons.entity.UserDO;
import com.example.commons.utils.HttpContextUtils;
import com.example.commons.utils.IPUtils;
import com.example.commons.utils.JSONUtils;
import com.example.jinzproduct.dao.LogDao;
import com.example.jinzproduct.server.LogService;
import com.example.jinzproduct.server.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * <pre>
 * </pre>
 * <small> 2018年3月22日 | Aron</small>
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogDao, LogDO> implements LogService {

    @Autowired
    private UserService userService;

    @Autowired
    private LogService logService;

    /**
     * 保存业务操作日志
     * @param logBO
     */
    @Override
    public void saveOperationLog(LogBO logBO) {
        //id为空则不记录业务日志
        if (StringUtils.isBlank(logBO.getUserId())){
            return;
        }
        LogDO sysLog = new LogDO();
        BeanUtil.copyProperties(logBO,sysLog);
        //业务能走到这说明流程执行成功存入0如果有问题会被全局异常捕获进行处理
        sysLog.setOperateState("0");
        UserDO user = userService.getByUserId(Long.valueOf(sysLog.getUserId()));
        //设置用户名称
        sysLog.setUsername(user.getUsername());
        //设置接口响应时间
        long beginTime = System.currentTimeMillis();
        long time = 0L;
        time = System.currentTimeMillis() - beginTime;
        sysLog.setTime((int)time);
        //设置用户机构名称
        sysLog.setDepartment(user.getOrgName());
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        if (request != null) {
            // 设置IP地址
            sysLog.setIp(IPUtils.getIpAddr(request));
            // 设置调用方法(get post)与url
            sysLog.setMethod(request.getMethod()+" "+request.getRequestURI());
            // 设置接口参数转为json格式
            Map<String, String[]> parameterMap = request.getParameterMap();
            if (parameterMap!=null){
                String params = JSONUtils.beanToJson1(parameterMap);
                sysLog.setParams(params);
            }
        }
        //设置日期
        sysLog.setGmtCreate(new Date());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //拼接并设置描述
        String depict =sysLog.getDepartment()+"部,"+""+"用户:"+sysLog.getUsername()+"(ip为:"
                +sysLog.getIp()+") 在"+sdf.format(sysLog.getGmtCreate())+" 调用了"+sysLog.getOperation()+"接口!";

        sysLog.setDepict(null != sysLog.getDepartment()&& "".equals(sysLog.getDepartment())?depict:depict.substring(depict.indexOf(",")+1));
        // 保存系统日志
        logService.save(sysLog);
    }
}
